<?php
/**
 * GData class (GData.php)
 *  Handle communications with Google - Zend GData API
 *
 *  intial creation 11/22/09
 *
 * @author Scott Szretter
 *
 * @todo add logging and/or error trapping
 */

 
class GData {
    public $GDataUser; //google docs username
    public $GDataPass; //google docs password

    public $GDataClient; //reference to the GData connection

    public $GDataSSKey; //google doc spreadsheet key (from it's url)
    public $GDataWkShtID; //google doc worksheet id (od6)
    public $GDataQueryString; //optional query string when getting rows

    public $QuestionsAvailable = false; //are there questions avaialble?


    function __construct()
    {
        //load the zend google data api code and classes
        require_once('Zend/Loader.php');  //could use ini_set and put zend in the include path instead
        Zend_Loader::loadClass('Zend_Gdata');
        Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
        Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');
    }
    
    function GDataAuthenticate()
    {
    // authenticate to google and get access to spreadsheets
    $authService = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
    $httpClient = Zend_Gdata_ClientLogin::getHttpClient($this->GDataUser, $this->GDataPass, $authService);
    $this->GDataClient = new Zend_Gdata_Spreadsheets($httpClient);
    }

    function UpdateSheet($row,$updatedRowArray)
    {
        return $this->GDataClient->updateRow($row, $updatedRowArray);
    }

public function listInsertAction($rowArray)
{
    $entry = $this->GDataClient->insertRow($rowArray, $this->GDataSSKey, $this->GDataWkShtID);
    if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry) {
      //  foreach ($rowArray as $column_header => $value) {
          //  echo "Success! Inserted '$value' in column '$column_header' at row ". substr($entry->getTitle()->getText(), 5) ."\n";
        return TRUE;
         }
         else
         {
             return FALSE; //falied?
         }
}

function getWkshtListFeed() {
    $query = new Zend_Gdata_Spreadsheets_ListQuery();
    $query->setSpreadsheetKey($this->GDataSSKey);
    $query->setWorksheetId($this->GDataWkShtID);
    
    if ($this->GDataQueryString !== null) {
        $query->setSpreadsheetQuery($this->GDataQueryString);
    }
    $listFeed = $this->GDataClient->getListFeed($query);
    return $listFeed;
}


//get a specific range of columns from a starting row
function getCellFeed($iMinCol, $iMaxCol, $iMinRow)
{
    $query = new Zend_Gdata_Spreadsheets_CellQuery();
    $query->setSpreadsheetKey($this->GDataSSKey);
    $query->setWorksheetId($this->GDataWkShtID);
    $query->setMinCol($iMinCol);
    $query->setMaxCol($iMaxCol);
    $query->setMinRow($iMinRow);

    return $this->GDataClient->getCellFeed($query);
}

public function printFeed($feed)
{
    $i = 0;
    foreach($feed->entries as $entry) {
        if ($entry instanceof Zend_Gdata_Spreadsheets_CellEntry) {
            print $entry->title->text .' '. $entry->content->text . "\n";
        } else if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry) {
            print $i .' '. $entry->title->text .' | '. $entry->content->text . "\n";
        } else {
            print $i .' '. $entry->title->text . "\n";
        }
        $i++;
    }
}

function feedToArray($feed)
{
    $returnArray = Array();
    $i = 0;
    foreach($feed->entries as $entry) {
        if ($entry instanceof Zend_Gdata_Spreadsheets_CellEntry) {
            $returnArray[] =  $entry->content->text;
        } else if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry) {
             $returnArray[] =  $entry->content->text;
        } else {
           $returnArray[] = $entry->title->text;
        }
        $i++;
    }

    return $returnArray;
}

function getUniqueTopics()
{
    $cellFeed = $this->getCellFeed(3, 3, 2); //column 3, starting at second row (skip header)
    $aTopics = array_unique($this->feedToArray($cellFeed));
    return $aTopics;
}


} //class
?>
